package com.dr.dac.knowledgebase.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.dr.dac.knowledgebase.R;
import com.dr.dac.knowledgebase.ui.bean.KClass;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weifeiyang on 2016/12/26.
 */

public class KClassAdapter extends RecyclerView.Adapter<KClassAdapter.MyViewHolder> {

    private Context mContext;
    private List<KClass> list;
    private MyItemClickListener myItemClickListener;

    public KClassAdapter(Context mContext) {
        list = new ArrayList<>();
        this.mContext = mContext;
    }

    public void setDatas(List<KClass> list) {
        this.list = list;
    }

    public void setOnItemClickListener(MyItemClickListener listener) {
        this.myItemClickListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = null;
        holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_kclass, parent,
                false), myItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.nameTv.setText(list.get(position).getFPointIDName());
        holder.midTv.setText(list.get(position).getClassMID());

        if(position==0){
            holder.topLayout.setVisibility(View.VISIBLE);
        }else{
            holder.topLayout.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView nameTv;
        TextView midTv;
        View topLayout;

        private MyItemClickListener listener;

        public MyViewHolder(View view, MyItemClickListener clickListener) {
            super(view);
            nameTv = (TextView) view.findViewById(R.id.name);
            midTv = (TextView) view.findViewById(R.id.mid);
            topLayout = view.findViewById(R.id.top_layout);
            this.listener = clickListener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClick(v, getPosition());
            }
        }
    }

    public interface MyItemClickListener {
        public void onItemClick(View view, int position);
    }
}
